var http = require('http');
var express = require('express')
var app = express();
var path = require('path')


var ejs = require('ejs');
app.set('views', path.join(__dirname, '../public'))
app.set('view engine', 'html')
app.engine('.html', ejs.__express)


/*add webpack*/
import webpack from 'webpack'
import webpackConfig from '../build/webpack.dev'

var compiler = webpack(webpackConfig)
var devMiddleware = require('webpack-dev-middleware')(compiler, {
    publicPath: "/",
})
var hotMiddleware = require('webpack-hot-middleware')(compiler, {
    log: () => {
    }
})
app.use(devMiddleware)
app.use(hotMiddleware)
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import { StaticRouter } from 'react-router'
import Routes from './client/route/routes.js'

/* 设置静态目录 */
console.log("static==>", path.resolve(__dirname, '../public'))

app.use(express.static(path.resolve(__dirname, '../public')));

app.get(/\/client*/, (req, res, next) => {
    var str = "hello this is server render";
    const context = {}
    str = ReactDOMServer.renderToString(<StaticRouter location={req.url} context={context}>
        <Routes />
    </StaticRouter>)
    return res.render('index', { root: str })
})


http.createServer(app).listen(9000);
console.log("server lisent on 9000")